package com.qiudaozhang.controller;

import com.qiudaozhang.dto.ResponseCode;
import com.qiudaozhang.model.DataDictionary;
import com.qiudaozhang.service.DataDictionaryService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("data")
public class DataController {
    @Autowired
    private DataDictionaryService dataDictionaryService;
    @RequestMapping("to/list")
    public String toList(){
        return "data/list";
    }
    @RequestMapping("to/add")
    public String toAdd(){
        return "data/add";
    }


    @RequestMapping("to/modify")
    public String toModify(@Param("id") Long id, Model model){
        DataDictionary d=dataDictionaryService.queryById(id);
        model.addAttribute("data",d);
        return "data/modify";
    }
    @RequestMapping("to/see")
    public String toSee(@Param("id") Long id, Model model){
        DataDictionary d=dataDictionaryService.queryById(id);
        model.addAttribute("data",d);
        return "data/see";
    }
    @RequestMapping("modify")
    public String modify(DataDictionary dataDictionary){
        dataDictionaryService.modify(dataDictionary);
        return "redirect:to/list";
    }
    @RequestMapping("del/{id}")
    @ResponseBody
    public ResponseCode del(@PathVariable("id") Integer id){
        ResponseCode code =dataDictionaryService.del(id);
        return code;
    }


    @RequestMapping("add")
    public String add(DataDictionary d){
        dataDictionaryService.add(d);
        return "redirect:to/list";
    }

    @RequestMapping("list")
    @ResponseBody
    public ResponseCode list(@RequestParam(value = "limit") Integer pageSize,
                             @RequestParam(value = "page") Integer pageNum){
        ResponseCode code = dataDictionaryService.query(pageSize,pageNum);
        return code;
    }



}
